package ee.subjects.backend.services;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import ee.subjects.backend.database.ConnectionManager;
import ee.subjects.backend.model.dao.SubjectSearchDAO;
import ee.subjects.backend.model.dao.Subject_attribute_type_DAO;
import ee.subjects.backend.model.dao.factory.DAOFactory;
import ee.subjects.backend.model.data.Address;
import ee.subjects.backend.model.data.Contact;
import ee.subjects.backend.model.data.Subject;
import ee.subjects.backend.model.data.Subject_attribute;
import ee.subjects.backend.model.data.Subject_attribute_type;

public class SubjectSearchServiceImpl implements SubjectSearchService
{
	Logger logger = Logger.getLogger(SubjectSearchServiceImpl.class);
	DAOFactory daoFactory = DAOFactory.getDAOFactory(); //PEAB ILMTINGIMATA ENNE ConnectionManageri olema SEST INITSIALISEERIB PROPERTIES ANDMED ConnectionManageri jaoks
	
	@Override
	public List<Subject_attribute_type> getAllSubject_attribute_typesForSpecificSubjectType(
			int subjectTypeId) 
	{
		Connection dbConnection = ConnectionManager.getInstance().getConnection();
		Subject_attribute_type_DAO subject_attribute_type_DAOImpl = daoFactory.getSubject_attribute_type_DAOImpl(dbConnection);
		
		List<Subject_attribute_type> listOfSubject_attribute_types = new ArrayList<Subject_attribute_type>();
		
		try 
		{
			listOfSubject_attribute_types = subject_attribute_type_DAOImpl.getAllSubject_attribute_typesForSpecificSubjectType(subjectTypeId);
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
			// TODO: handle exception
		}
		finally
		{
			ConnectionManager.getInstance().closeConnection();
		}
		
		return listOfSubject_attribute_types;
	}
	
	@Override
	public List<Subject> findSubjectsMatchingTheCriterias(
			Subject subjectCriteria, int subjectTypeToSearchFor,
			Contact contactCriteria, Address addressCriteria,
			List<Subject_attribute> listOfSubject_attributeCriterias) 
	{
		Connection dbConnection = ConnectionManager.getInstance().getConnection();
		SubjectSearchDAO subjectSearchDAOImpl = daoFactory.getSubjectSearchDAOImpl(dbConnection);
		
		List<Subject> listOfSubjectsRetrieved = new ArrayList<Subject>();
		
		try 
		{
			listOfSubjectsRetrieved = subjectSearchDAOImpl.findSubjectsMatchingTheCriterias(subjectCriteria, 
					subjectTypeToSearchFor, contactCriteria, addressCriteria, listOfSubject_attributeCriterias);
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
			// TODO: handle exception
		}
		finally
		{
			ConnectionManager.getInstance().closeConnection();
		}
		// TODO Auto-generated method stub
		return listOfSubjectsRetrieved;
	}
	
}
